package com.yg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yg.mapper.TBookMapper;
import com.yg.mapper.TOrderItemMapper;
import com.yg.mapper.TUserMapper;
import com.yg.pojo.*;
import com.yg.mapper.TOrderMapper;
import com.yg.pojo.vo.PageVo;
import com.yg.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yg.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kotori
 * @since 2022-11-18
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private TUserMapper userMapper;
    @Autowired
    private TOrderItemMapper orderItemMapper;
    @Autowired
    private TOrderMapper orderMapper;
    @Autowired
    private TBookMapper bookMapper;
    @Autowired
    private PageUtil<TOrder> pageUtil;

    @Override
    public String createOrder(Cart cart, String user) {
        LambdaQueryWrapper<TUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TUser::getUsername, user);
        Integer userId = userMapper.selectOne(lambdaQueryWrapper).getId();
        String orderId = System.currentTimeMillis() + "" + userId;
        TOrder order = new TOrder(orderId, new Timestamp(System.currentTimeMillis()), cart.getTotalPrice(), 0, userId);
        orderMapper.insert(order);
        for (Map.Entry<Integer, CartItem> entry : cart.getItems().entrySet()) {
            CartItem cartItem = entry.getValue();
            TOrderItem orderItem = new TOrderItem(null, cartItem.getName(), cartItem.getCount(), cartItem.getPrice(), cartItem.getTotalPrice(), orderId);
            orderItemMapper.insert(orderItem);
            TBook book = bookMapper.selectById(cartItem.getId());
            book.setSales(book.getSales() + cartItem.getCount());
            book.setStock(book.getStock() - cartItem.getCount());
            bookMapper.updateById(book);
        }
        cart.clear();
        return orderId;
    }

    @Override
    public PageVo<TOrder> getMyOrderPage(Long pageNo, Long pageSize, Integer userId, String url) {
        if (pageNo == 0) {
            pageNo = 1L;
        }
        if (pageSize == 0) {
            pageSize = 4L;
        }
        LambdaQueryWrapper<TOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TOrder::getUserId, userId);
        Page<TOrder> page = orderMapper.selectPage(new Page<>(pageNo, pageSize), lambdaQueryWrapper);
        return pageUtil.Page2PageVo(page, url);
    }

    @Override
    public PageVo<TOrder> allOrderPage(Long pageNo, Long pageSize, String url) {
        if (pageNo == 0) {
            pageNo = 1L;
        }
        if (pageSize == 0) {
            pageSize = 4L;
        }
        Page<TOrder> page = orderMapper.selectPage(new Page<>(pageNo, pageSize), null);
        return pageUtil.Page2PageVo(page, url);
    }

    @Override
    public Boolean fahuo(String orderId) {
        TOrder order = new TOrder(orderId, null, null, 1, null);
        int i = orderMapper.updateById(order);
        return i > 0;
    }
}
